package com.le.tester.lock;

import com.le.tester.dao.DistributeLockDao;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.locks.Condition;

/**
 * createTime：2025-03-31 14:48
 * description：主要是sql中加锁，然后java中通过事务来控制锁
 */
public class MysqlLock extends AbstractLock {

    public DistributeLockDao distributeLockDao;

    public DataSourceTransactionManager transactionManager;

    public TransactionStatus status;

    @Override
    public void lockInterruptibly() throws InterruptedException {

    }

    public MysqlLock(DistributeLockDao distributeLockDao, String lockName, DataSourceTransactionManager transactionManager) {
        this.lockName = lockName;
        this.distributeLockDao = distributeLockDao;
        this.transactionManager = transactionManager;
    }

    @Override
    public boolean tryLock() {
        return false;
    }

    @Override
    public Condition newCondition() {
        return null;
    }

    public void lock() {
        //事务定义，用来定义事务隔离级别，事务传播行为等等
        DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        status = transactionManager.getTransaction(definition);
        while (true) {
            try {
                distributeLockDao.selectLockName(lockName);
//                return;
            } catch (Exception e) {
                return;
            }
        }

    }


    @Override
    public void unlock() {

        transactionManager.commit(status);
    }
}
